﻿using System.Collections.Generic;
using System.Linq;
using SurveySez.Model;
using UpdateControls;

namespace SurveySez.NavigationModel
{
    public class GameNavigation
	{
		// Immutable
		private Game _game;

		// Independent
		private int _currentRound;
		private Independent _indCurrentRound = new Independent();

		// Dependent
		private List<RoundNavigation> _rounds;
		private Dependent _depRounds;

		public GameNavigation(Game game)
		{
			_game = game;

			_depRounds = new Dependent(UpdateRounds);
		}

		private void UpdateRounds()
		{
			using (var bin = _rounds.Recycle())
			{
				_rounds = _game.GameSurveys
					.OrderBy(gs => gs.Order)
					.Select(gs => gs.Survey)
					.Select(s => bin.Extract(new RoundNavigation(s)))
					.ToList();
			}
		}

		public void NextRound()
		{
			if (_currentRound < _game.GameSurveys.Count() - 1)
			{
				_indCurrentRound.OnSet();
				_currentRound++;
			}
		}

		public void PreviousRound()
		{
			if (_currentRound > 0)
			{
				_indCurrentRound.OnSet();
				_currentRound--;
			}
		}

		public RoundNavigation CurrentRound
		{
			get
			{
				_depRounds.OnGet();
				_indCurrentRound.OnGet();
				return _rounds.ElementAt(_currentRound);
			}
		}
	}
}
